package com.zhixun.mobile.modules.site.web;

import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.boxin.ims.modules.momarketing.entity.CustomPage;
import com.boxin.ims.modules.momarketing.service.CustomPageService;
import com.boxin.ims.modules.wechat.entity.WeChat;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.WeChatService;
import com.boxin.ims.modules.wechat.service.WechatDaoHelper;
import com.boxin.ims.modules.wechat.utils.AssertUtils;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.CookieUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.zhixun.mobile.modules.base.web.BaseController;
import com.zhixun.mobile.modules.member.web.IndexController;
import com.zhixun.mobile.sys.utils.SessionMemberUtils;
/**  
* @author Administrator  
*    
*/
/**  
* @author Administrator  
*    
*/
@Controller
@RequestMapping(value= Global.FRONT_PATH+ "/site")  /* site */
public class MicroSiteController extends BaseController {
	
	@Autowired
	private CustomPageService customPageService;
	
	@PersistenceContext
	private EntityManager entityManager;
	
	@Autowired
	SystemService systemService;
	
	@Autowired
	WechatDaoHelper wechatDaoHelper;
	
	@Autowired
	private WeChatService weChatService;
	
	
	private Logger logger = Logger.getLogger(IndexController.class);
	
	
	
	
	@RequestMapping(value = "toRichpage/{id}")
	public String viewRichCustomPage(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response){
		CustomPage page = customPageService.get(id);
		
		String uid = request.getParameter("uid");
		String wechatId = request.getParameter("wechatId");
		
		System.out.println("wechatId 1 :   "+wechatId+"%%%%%%%%%%%%%%%%%%%%");

		if(StringUtils.isBlank(wechatId)){
			WechatMember member = SessionMemberUtils.getSessionMemberByUserId(request, uid);
			System.out.println("wechatId  :   "+wechatId+"%%%%%%%%%%%%%%%%%%%%");
			
			if (AssertUtils.isNull(member)) {
				String url = getOAuthUrl("/site/toRichpage/"+id+"?a=1",uid,request,response);
				return "redirect:"+url;
			}			
		}
		
		System.out.println("wechatId  :   "+wechatId+"#########################");
		request.setAttribute("customPage", page);
		request.setAttribute("wechatId", wechatId);
		request.setAttribute("uid", uid);
		request.setAttribute("mobile", request.getParameter("mobile"));
		request.setAttribute("topage", request.getParameter("topage"));
		
		return "modules/site/richCustomPage";
	}
	
	
	
	
	/**
	 * 购物车商品数量
	  * @function 
	  * @author lzx
	  * @time 2014-11-11 下午2:09:44
	  * @param model
	  * @param request
	  * @param response
	  * @return
	 */
	public int shoppingCartNumber(HttpServletRequest request, HttpServletResponse response){
		String products = CookieUtils.getCookie(request, "shoppingCart");
		System.out.println(products);
		Integer tatol=0;//数量
		if(StringUtils.isNotBlank(products)){
			Map<String, Object> maps = JSON.parseObject(products);
			if(maps!=null){
				for (Object o : maps.entrySet()) { 
				      Map.Entry<Long,Integer> entry = (Map.Entry<Long,Integer>)o; 
				      tatol+=entry.getValue();
				}
			}
		}
		return tatol;
	}
	
	

	/**
	 * 获得oauth路径     url传入    /site/shopping
	 * @param url
	 * @param uid
	 * @param request
	 * @return
	 */
	public String getOAuthUrl(String url,String uid,HttpServletRequest request,HttpServletResponse response){

		String path = request.getContextPath();
		String basePath  = request.getScheme()+"://"+request.getServerName()+path;

		WeChat weChat =  weChatService.getWeChatByUserId(Long.parseLong(uid));
		String state = uid;
		url = url.replace("null", "");
		CookieUtils.setCookie(response, "redirectUrl", basePath+Global.getFrontPath()+url);
		
		System.out.println(basePath);

		String redirectUrl =URLEncoder.encode(basePath+"/f/oauth/redirect");

		String returnUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+weChat.getAppId()
					+"&redirect_uri="+redirectUrl+"&response_type=code&scope=snsapi_base&state="+state
					+"&connect_redirect=1#wechat_redirect";
		return returnUrl;
	}
}

